/*
 * COisBufferedInputHandler.cpp
 *
 *  Created on: 28.04.2009
 *      Author: markus
 */

#include "COisBufferedInputHandler.h"

COisBufferedInputHandler::COisBufferedInputHandler(std::string strPar1, std::string strPar2, CLivingObject *player)
{
	OIS::ParamList pl;
	m_bContinue = true;

	m_pKeyboard = NULL;
	m_pMouse = NULL;
	m_pInputMgr = NULL;


	pl.insert(std::make_pair(strPar1, strPar2));
	m_pInputMgr = OIS::InputManager::createInputSystem(pl);


	writeDebugOutput("COisBufferedInputHandler::COisBufferedInputHandler(): Setting up input handler.");
    try
    {
    	writeDebugOutput("COisBufferedInputHandler::COisBufferedInputHandler(): creating keyboard");
        m_pKeyboard = static_cast<OIS::Keyboard*>(m_pInputMgr->createInputObject(OIS::OISKeyboard, true));
    	writeDebugOutput("COisBufferedInputHandler::COisBufferedInputHandler(): creating mouse");
        m_pMouse = static_cast<OIS::Mouse*>(m_pInputMgr->createInputObject(OIS::OISMouse, true));
    }
    catch (const OIS::Exception &e)
    {
    	writeExceptionOutput("COisBufferedInputHandler::COisBufferedInputHandler: An exception occurred while creating the input system");
    	writeExceptionOutput(e.eText);
    	throw Exception(42, e.eText, "Application::setupInputSystem");
    }

	writeDebugOutput("COisBufferedInputHandler::COisBufferedInputHandler(): checking for keyboard");
    if(m_pKeyboard != NULL)
    {
		writeDebugOutput("COisBufferedInputHandler::COisBufferedInputHandler(): setting keyboard event callback");
    	m_pKeyboard->setEventCallback(this);
    }

    writeDebugOutput("COisBufferedInputHandler::COisBufferedInputHandler(): set mouse event callback");
    if(m_pMouse)
    	m_pMouse->setEventCallback(this);

	writeDebugOutput("COisBufferedInputHandler::COisBufferedInputHandler(): set internal player variable");

    m_pPlayer = player;

}

COisBufferedInputHandler::~COisBufferedInputHandler()
{
	if(m_pKeyboard)
		m_pInputMgr->destroyInputObject(m_pKeyboard);

	if(m_pMouse)
		m_pInputMgr->destroyInputObject(m_pMouse);

	if(m_pInputMgr)
		OIS::InputManager::destroyInputSystem(m_pInputMgr);
}


bool COisBufferedInputHandler::keyPressed(const OIS::KeyEvent &ev)
{
//	writeDebugOutput("COisBufferedInputHandler::keyPressed(): catching keyboard event.");
//	float fMoveConst = 0.0;

//	if(!m_pPlayer->getMoveConst(&fMoveConst))
//	{
//		writeDebugOutput("COisBufferedInputHandler::keyPressed(): could not get move and rotation constants out of player object!");
//		return false;
//	}

	m_bContinue = true;

	switch(ev.key)
	{
		case OIS::KC_D:
		{
			m_pPlayer->MoveX(1);
			break;
		}
		case OIS::KC_A:
		{
			m_pPlayer->MoveX(-1);
			break;
		}
		case OIS::KC_SPACE:
		{
			m_pPlayer->MoveY(1);
			break;
		}
		case OIS::KC_LCONTROL:
		{
			m_pPlayer->MoveY(-1);
			break;
		}
		case OIS::KC_S:
		{
			m_pPlayer->MoveZ(1);
			break;
		}
		case OIS::KC_W:
		{
			m_pPlayer->MoveZ(-1);
			break;
		}
		case OIS::KC_ESCAPE:
		{
			writeDebugOutput("COisBufferedInputHandler::keyPressed(): catching ESC");
			m_bContinue = false;
//			break;
		}
		case OIS::KC_LSHIFT:
		{
			m_pPlayer->setSprintMove(true);
			break;
		}
	}

	return true;
}

bool COisBufferedInputHandler::keyReleased(const OIS::KeyEvent &ev)
{
//	float fMoveConst = 0.0;

//	if(!m_pPlayer->getMoveConst(&fMoveConst))
//	{
//		writeDebugOutput("COisBufferedInputHandler::keyPressed(): could not get move and rotation constants out of player object!");
//		return false;
//	}

	switch(ev.key)
	{
		case OIS::KC_D:
		{
			m_pPlayer->MoveX(-1);
			break;
		}
		case OIS::KC_A:
		{
			m_pPlayer->MoveX(1);
			break;
		}
		case OIS::KC_SPACE:
		{
			m_pPlayer->MoveY(-1);
			break;
		}
		case OIS::KC_LCONTROL:
		{
			m_pPlayer->MoveY(1);
			break;
		}
		case OIS::KC_S:
		{
			m_pPlayer->MoveZ(-1);
			break;
		}
		case OIS::KC_W:
		{
			m_pPlayer->MoveZ(1);
			break;
		}
		case OIS::KC_ESCAPE:
		{
			break;
		}
		case OIS::KC_LSHIFT:
		{
			m_pPlayer->setSprintMove(false);
			break;
		}
	}

	return true;
}

bool COisBufferedInputHandler::mousePressed(const OIS::MouseEvent &ev, OIS::MouseButtonID id)
{

	return true;
}

bool COisBufferedInputHandler::mouseReleased(const OIS::MouseEvent &ev, OIS::MouseButtonID id)
{
	return true;
}

bool COisBufferedInputHandler::mouseMoved(const OIS::MouseEvent &ev)
{
	m_pPlayer->Rotate(ev.state.X.rel, ev.state.Y.rel, 0);

	return true;
}
